#!/bin/sh
#
# Copyright 2021 EMQ Technologies Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# postinst script for kuiper
#
# see: dh_installdeb(1)

set -e -u

if command -v systemctl >/dev/null 2>&1; then
    systemctl enable kuiper
fi

# create group
if ! getent group kuiper >/dev/null; then
        addgroup --system kuiper
fi

# create user
if ! getent passwd kuiper >/dev/null; then
        adduser --ingroup kuiper \
                --home /var/lib/kuiper \
                --disabled-password \
                --system --shell /bin/bash --no-create-home \
                --gecos "kuiper user" kuiper
fi

chown -R kuiper:kuiper /var/lib/kuiper
chown -R kuiper:kuiper /usr/lib/kuiper
chown -R kuiper:kuiper /etc/kuiper

chmod 0755 /etc/kuiper
chmod 0644 /etc/kuiper/*
chmod -R +X /etc/kuiper
chmod -R 0755 /usr/lib/kuiper/bin
[ -f /usr/bin/kuiperd ] && rm /usr/bin/kuiperd
[ -f /usr/bin/kuiper ] && rm /usr/bin/kuiper
ln -s /usr/lib/kuiper/bin/kuiperd /usr/bin/kuiperd
ln -s /usr/lib/kuiper/bin/kuiper /usr/bin/kuiper

case "$1" in
    configure)
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
